// pages/storeManges/storeManges.js
const app = getApp();
import httpsUtils from '../../utils/https.js'; 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataImg:[],
    shopInfo:{},
    shopID:'',
    levelType: 1,//默认1表示星星，2表示钻石，3表示皇冠
    levelArray: [1],//默认1
    plComment: [],

    focusStatus: 2,//默认2表示不关注 1表示关注
  },
  /**
  * 关注或取消关注
  */
  updateFocusShop: function () {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/focusShop/updateOrAdd?shopId=' + that.data.shopID,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        if (json.data.status == 1) {
          wx.showToast({
            title: '关注成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '取消关注成功',
            icon: 'none'
          })
        }
        that.setData({
          focusStatus: json.data.status,
        })
      }
    })
  },
  /**
   * 店铺是否关注
   */
  getFocusShop: function () {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/shopInfo/isFocusShop?id=' + that.data.shopID,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          focusStatus: json.data.status,
        })
      }
    })
  },
  //设置 荣誉等级转换
   shopGrade: function (creditLevel) {
    var that = this;
    var levelType = parseInt(creditLevel / 5);
    var levelTimes = parseInt(creditLevel % 5);
    var levelArray = [];
    for (var i = 0; i < levelTimes; i++) {
      levelArray.push(levelType);
    }
    that.setData({
      levelType: levelType,
      levelArray: levelArray
    })
  },

  // 渲染页面的基本内容
  getShopInfo: function () {
    var that=this;
    httpsUtils.getData({
      _url: app.globalData._url + '/shopInfo/selectagno1?shopId=' + that.data.shopID,
      data: {},
    }, function (json) {
      var imgUrls = [];
      if (json.data.storeImg != null) {
        imgUrls = json.data.storeImg.split("***");
      }
      if (json.data && json.success) {
        //设置荣誉等级
        that.shopGrade(json.data.creditLevel);

        that.setData({
          shopInfo: json.data,
          dataImg: imgUrls,
        })
      } else {
        console.log("请求失败")
      }
    })
  },
  //店铺好评率
  findComment: function () {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + `/comment/getCommentByCommentLevel?shopId=${that.data.shopID }` ,
      data: '',
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          plComment:json.data.list
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      shopID: options.shopId,
    })
    that.getShopInfo();
    that.findComment();
    that.getFocusShop();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})